home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / sed.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  45KB  |  1,584 lines

  1. /* sed - stream editor        Author: Eric S. Raymond */
  2.  
  3. /* This used to be three different files with the following makefile:
  4.  * (Note the chmem).
  5.  
  6. CFLAGS=    -F -T.
  7.  
  8. OBJS=   sedcomp.s sedexec.s
  9.  
  10. sed:     $(OBJS)
  11.         cc -T. -o sed $(OBJS)
  12.   @chmem =13312 sed
  13.  
  14. $(OBJS):    sed.h
  15.  
  16.  * If you want longer lines: increase MAXBUF.
  17.  * If you want scripts with more text: increase POOLSIZE.
  18.  * If you want more commands per script: increase MAXCMDS.
  19.  */
  20.  
  21. #include <ctype.h>
  22. #include <stdio.h>
  23.  
  24. /*+++++++++++++++*/
  25.  
  26. /* Sed.h -- types and constants for the stream editor */
  27.  
  28. /* Data area sizes used by both modules */
  29. #define MAXBUF        4000    /* current line buffer size */
  30. #define MAXAPPENDS    20    /* maximum number of appends */
  31. #define MAXTAGS        9    /* tagged patterns are \1 to \9 */
  32.  
  33. /* Constants for compiled-command representation */
  34. #define EQCMD    0x01        /* = -- print current line number     */
  35. #define ACMD    0x02        /* a -- append text after current line     */
  36. #define BCMD    0x03        /* b -- branch to label             */
  37. #define CCMD    0x04        /* c -- change current line         */
  38. #define DCMD    0x05        /* d -- delete all of pattern space */
  39. #define CDCMD    0x06        /* D -- delete first line of pattern space */
  40. #define GCMD    0x07        /* g -- copy hold space to pattern space */
  41. #define CGCMD    0x08        /* G -- append hold space to pattern space */
  42. #define HCMD    0x09        /* h -- copy pattern space to hold space */
  43. #define CHCMD    0x0A        /* H -- append pattern space to hold space */
  44. #define ICMD    0x0B        /* i -- insert text before current line     */
  45. #define LCMD    0x0C        /* l -- print pattern space in escaped form */
  46. #define NCMD    0x0D        /* n -- get next line into pattern space */
  47. #define CNCMD    0x0E        /* N -- append next line to pattern space */
  48. #define PCMD    0x0F        /* p -- print pattern space to output     */
  49. #define CPCMD    0x10        /* P -- print first line of pattern space */
  50. #define QCMD    0x11        /* q -- exit the stream editor         */
  51. #define RCMD    0x12        /* r -- read in a file after current line */
  52. #define SCMD    0x13        /* s -- regular-expression substitute     */
  53. #define TCMD    0x14        /* t -- branch on any substitute successful */
  54. #define CTCMD    0x15        /* T -- branch on any substitute failed     */
  55. #define WCMD    0x16        /* w -- write pattern space to file     */
  56. #define CWCMD    0x17        /* W -- write first line of pattern space */
  57. #define XCMD    0x18        /* x -- exhange pattern and hold spaces     */
  58. #define YCMD    0x19        /* y -- transliterate text         */
  59.  
  60. struct cmd_t {            /* compiled-command representation */
  61.   char *addr1;            /* first address for command */
  62.   char *addr2;            /* second address for command */
  63.   union {
  64.     char *lhs;        /* s command lhs */
  65.     struct cmd_t *link;    /* label link */
  66.   } u;
  67.   char command;            /* command code */
  68.   char *rhs;            /* s command replacement string */
  69.   FILE *fout;            /* associated output file descriptor */
  70.   struct {
  71.     char allbut;        /* was negation specified? */
  72.     char global;        /* was g postfix specified? */
  73.     char print;        /* was p postfix specified? */
  74.     char inrange;        /* in an address range? */
  75.   } flags;
  76. };
  77. typedef struct cmd_t sedcmd;    /* use this name for declarations */
  78.  
  79. #define BAD    ((char *) -1)    /* guaranteed not a string ptr */
  80.  
  81.  
  82.  
  83. /* Address and regular expression compiled-form markers */
  84. #define STAR    1        /* marker for Kleene star */
  85. #define CCHR    2        /* non-newline character to be matched
  86.              * follows */
  87. #define CDOT    4        /* dot wild-card marker */
  88. #define CCL    6        /* character class follows */
  89. #define CNL    8        /* match line start */
  90. #define CDOL    10        /* match line end */
  91. #define CBRA    12        /* tagged pattern start marker */
  92. #define CKET    14        /* tagged pattern end marker */
  93. #define CBACK    16        /* backslash-digit pair marker */
  94. #define CLNUM    18        /* numeric-address index follows */
  95. #define CEND    20        /* symbol for end-of-source */
  96. #define CEOF    22        /* end-of-field mark */
  97.  
  98. /* Sed.h ends here */
  99.  
  100. #ifndef CMASK
  101. #define CMASK  0xFF        /* some char type should have been unsigned
  102.              * char? */
  103. #endif
  104.  
  105. /*+++++++++++++++*/
  106.  
  107. /* Sed - stream editor        Author: Eric S. Raymond */
  108.  
  109. /*
  110.    The stream editor compiles its command input     (from files or -e options)
  111.    into an internal form using compile() then executes the compiled form using
  112.    execute(). Main() just initializes data structures, interprets command line
  113.    options, and calls compile() and execute() in appropriate sequence.
  114.  
  115.    The data structure produced by compile() is an array of compiled-command
  116.    structures (type sedcmd).  These contain several pointers into pool[], the
  117.    regular-expression and text-data pool, plus a command code and g & p flags.
  118.    In the special case that the command is a label the struct  will hold a ptr
  119.    into the labels array labels[] during most of the compile,  until resolve()
  120.    resolves references at the end.
  121.  
  122.    The operation of execute() is described in its source module.
  123. */
  124.  
  125. /* #include <stdio.h>        /* uses getc, fprintf, fopen, fclose */
  126. extern FILE *fopen();        /* should this be in stdio.h? */
  127. /* #include "sed.h"        /* command type struct and name defines */
  128.  
  129. /* Imported functions */
  130. extern int strcmp();        /* test strings for equality */
  131. extern void execute();        /* execute compiled command */
  132.  
  133. /***** public stuff ******/
  134.  
  135. #define MAXCMDS        500    /* maximum number of compiled commands */
  136. #define MAXLINES    256    /* max # numeric addresses to compile */
  137.  
  138. /* Main data areas */
  139. char linebuf[MAXBUF + 1];    /* current-line buffer */
  140. sedcmd cmds[MAXCMDS + 1];    /* hold compiled commands */
  141. long linenum[MAXLINES];        /* numeric-addresses table */
  142.  
  143. /* Miscellaneous shared variables */
  144. int nflag;            /* -n option flag */
  145. int eargc;            /* scratch copy of argument count */
  146. char **eargv;            /* scratch copy of argument list */
  147. char bits[] = {1, 2, 4, 8, 16, 32, 64, 128};
  148.  
  149. /***** module common stuff *****/
  150.  
  151. #define POOLSIZE    20000    /* size of string-pool space */
  152. #define WFILES        10    /* max # w output files that can be compiled */
  153. #define RELIMIT        256    /* max chars in compiled RE */
  154. #define MAXDEPTH    20    /* maximum {}-nesting level */
  155. #define MAXLABS        50    /* max # of labels that can be handled */
  156.  
  157. #define SKIPWS(pc)    while ((*pc==' ') || (*pc=='\t')) pc++
  158. #define ABORT(msg)    (fprintf(stderr, msg, linebuf), quit(2))
  159. #define IFEQ(x, v)    if (*x == v) x++ ,    /* do expression */
  160.  
  161. /* Error messages */
  162. static char AGMSG[] = "sed: garbled address %s\n";
  163. static char CGMSG[] = "sed: garbled command %s\n";
  164. static char TMTXT[] = "sed: too much text: %s\n";
  165. static char AD1NG[] = "sed: no addresses allowed for %s\n";
  166. static char AD2NG[] = "sed: only one address allowed for %s\n";
  167. static char TMCDS[] = "sed: too many commands, last was %s\n";
  168. static char COCFI[] = "sed: cannot open command-file %s\n";
  169. static char UFLAG[] = "sed: unknown flag %c\n";
  170. static char CCOFI[] = "sed: cannot create %s\n";
  171. static char ULABL[] = "sed: undefined label %s\n";
  172. static char TMLBR[] = "sed: too many {'s\n";
  173. static char FRENL[] = "sed: first RE must be non-null\n";
  174. static char NSCAX[] = "sed: no such command as %s\n";
  175. static char TMRBR[] = "sed: too many }'s\n";
  176. static char DLABL[] = "sed: duplicate label %s\n";
  177. static char TMLAB[] = "sed: too many labels: %s\n";
  178. static char TMWFI[] = "sed: too many w files\n";
  179. static char REITL[] = "sed: RE too long: %s\n";
  180. static char TMLNR[] = "sed: too many line numbers\n";
  181. static char TRAIL[] = "sed: command \"%s\" has trailing garbage\n";
  182.  
  183. typedef struct {        /* represent a command label */
  184.   char *name;            /* the label name */
  185.   sedcmd *last;            /* it's on the label search list */
  186.   sedcmd *address;        /* pointer to the cmd it labels */
  187. }
  188.  
  189.  label;
  190.  
  191. /* Label handling */
  192. static label labels[MAXLABS];    /* here's the label table */
  193. static label *lab = labels + 1;    /* pointer to current label */
  194. static label *lablst = labels;    /* header for search list */
  195.  
  196. /* String pool for regular expressions, append text, etc. etc. */
  197. static char pool[POOLSIZE];    /* the pool */
  198. static char *fp = pool;        /* current pool pointer */
  199. static char *poolend = pool + POOLSIZE;    /* pointer past pool end */
  200.  
  201. /* Compilation state */
  202. static FILE *cmdf = NULL;    /* current command source */
  203. static char *cp = linebuf;    /* compile pointer */
  204. static sedcmd *cmdp = cmds;    /* current compiled-cmd ptr */
  205. static char *lastre = NULL;    /* old RE pointer */
  206. static int bdepth = 0;        /* current {}-nesting level */
  207. static int bcount = 0;        /* # tagged patterns in current RE */
  208.  
  209. /* Compilation flags